<?php
namespace Cyfy\Modules\Breadcrumbs2;

/**
 * Breadcrumbs navigation with custom names.
 *
 * @author Dandelion
 * @version 0.1
 */  
class Module extends \Cyfy\Module
{
    /**
     * Glue, which connects all values.
     * @var string
     */
    static public $glue = " &gt; ";
    
    /**
     * AJAX callback
     * @return mixed
     */    
	static function ajax($name){
		if($name == "save"){
			return self :: save();
		}
	}
    
    /**
     * Blocks.
     * @param string If name == breadcrumbs, returns HTML string, else array
     * @return mixed
     */	
    static function blocks($name = "")
    {
        if($name == "breadcrumbs")
        {
            $output = self :: breadcrumbs(\Cyfy\Cyfy ::  $query);
            // if breadcrumbs -> admin
            if(\Cyfy\Cyfy ::  permission("Breadcrumbs2", "edit breadcrumbs"))
            {
                \Cyfy\Cyfy ::  css(\Cyfy\Cyfy :: getSourcePath("Cyfy/Modules/Breadcrumbs2/admin.css"), "file");
                \Cyfy\Cyfy ::  javaScript(\Cyfy\Cyfy :: getSourcePath("Cyfy/Modules/Breadcrumbs2/admin.js"), "file");
            }
            return $output;
        }
        return array("breadcrumbs" => self :: t(10));
    }
 
    /**
     * Saves posted breadcrumbs
     */
    static function save()
    {
        if(!\Cyfy\Cyfy ::  permission("Breadcrumbs2", "edit breadcrumbs")){
            \Cyfy\Cyfy ::  noPermission();
            return false;
        }
        // permission OK
        foreach ($_POST["breadcrumbs"] as $link => $name)
        {
            $values = array(
                'language' => \Cyfy\Language :: getActive(),
                'link' => $link,
                'name' => $name
            );
            
            \dibi :: query("REPLACE INTO [:cyfy:Breadcrumbs2] %v", $values);
        }
        \Cyfy\Message :: set(self :: t(20));
    }
  
    /**
     * Returns breadcrumbs
     * @return string HTML output
     */  
    static function breadcrumbs($parse)
    {
        $items = explode("/", $parse);  
        $previous = "";
        $query = "";
        $edit = "";
        $out = "";
        $last = -1;
        
        foreach ($items as $int => $item) {
            $item = $previous . $item;
            $query[$int] = "'".$item."'";
            $previous = $item . "/";
            
            if($item != "")
            {
                $last = $int;
            }
        }
        
        $result = \dibi :: query("SELECT [name], [link] FROM [:cyfy:Breadcrumbs2] WHERE [link] IN('index',".implode(",", $query).") AND [language] = %s", \Cyfy\Language :: getActive());
            
        // loads crumbs
        $crumbs = array();
        foreach($result as $c)
        {
            $crumbs[$c["link"]] = $c["name"];
        }
        
        // if not index first - set index as first
        if($items[0]!="index"){
            array_unshift($items, "index");
            $last++;
        }    
        
        $previous = ""; // links
        
        foreach($items as $int => $item)
        {
            if($previous == "index/"){
                $previous = "";
            }
            
            // empty
            if($item == "")
            {
                continue;
            }

			// if in DB
            if($crumbs[$previous.$item])
			{
                $value = $crumbs[$previous.$item];
            }
			// else show default
			else
			{   // if numeric, use caption "Item "
				if(is_numeric($item))
				{
					$value = self::t(30, array($item));
				}
				else
				{
					$value = $item;
				}
            }
            
            // edit
            if(\Cyfy\Cyfy ::  permission("Breadcrumbs2", "edit breadcrumbs"))
            {
                $edit .= "<input type=\"text\" name=\"breadcrumbs[{$previous}{$item}]\" value=\"{$value}\" />";
            }
            
            // last? - go STRONG            
            if($last == $int){
                $value = "<strong>" . $value . "</strong>";
            }
            
            $out .= "<a href=\"" . \Cyfy\Cyfy :: getPath($previous . $item) . "\">" . $value . "</a>" . self :: $glue;
            $previous .= $item . "/";
        }
        
        // setting the output
        $output = "<div id=\"breadcrumbsContent\">" . self :: t(60) . substr($out, 0, -strlen( self :: $glue));
        if(\Cyfy\Cyfy ::  permission("Breadcrumbs2", "edit breadcrumbs")){
            $output .= "<strong style=\"padding-left:30px;\"><a href=\"#\" id=\"editBreadcrumbsBtn\">". self::t(50)."</a> </strong></div><div id=\"breadcrumbsEdit\" style=\"display:none;\" >" . $edit . "<input type=\"submit\" id=\"saveBreadcrumbsBtn\" value=\"". self ::t(40)."\" /> \n";
        }
        $output .= "</div>\n";
        
        return $output;
    }
}

  

  
